package yk.flutter.platelpr;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * 车牌限定框自定义View
 * 在相机预览上方绘制车牌识别引导框
 */
public class PlateFrameView extends View {
    
    private static final String TAG = "PlateFrameView";
    
    // 绘制相关
    private Paint framePaint;           // 边框画笔
    private Paint cornerPaint;          // 四角标记画笔
    private Paint textPaint;            // 文字画笔
    private Paint maskPaint;            // 遮罩画笔
    
    // 尺寸相关
    private int frameWidth = 300;       // 车牌框宽度 (dp) - 从320增加到380
    private int frameHeight = 120;      // 车牌框高度 (dp) - 从80增加到120
    private int cornerLength = 25;      // 四角标记长度 (dp) - 从20增加到25
    private int frameStrokeWidth = 3;   // 边框线宽 (dp) - 从2增加到3
    private int cornerStrokeWidth = 4;  // 四角标记线宽 (dp) - 从3增加到4
    
    // 颜色
    private int frameColor = Color.parseColor("#2196F3");
    private int cornerColor = Color.parseColor("#FF2196F3");
    private int textColor = Color.WHITE;
    private int maskColor = Color.parseColor("#80000000");
    
    // 位置
    private RectF frameRect;
    private String hintText = "请将车牌对准识别框";
    
    public PlateFrameView(Context context) {
        super(context);
        init();
    }
    
    public PlateFrameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public PlateFrameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    /**
     * 初始化画笔和参数
     */
    private void init() {
        // 转换dp到px
        frameWidth = dpToPx(frameWidth);
        frameHeight = dpToPx(frameHeight);
        cornerLength = dpToPx(cornerLength);
        frameStrokeWidth = dpToPx(frameStrokeWidth);
        cornerStrokeWidth = dpToPx(cornerStrokeWidth);
        
        // 初始化边框画笔
        framePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        framePaint.setColor(frameColor);
        framePaint.setStyle(Paint.Style.STROKE);
        framePaint.setStrokeWidth(frameStrokeWidth);
        
        // 初始化四角标记画笔
        cornerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        cornerPaint.setColor(cornerColor);
        cornerPaint.setStyle(Paint.Style.STROKE);
        cornerPaint.setStrokeWidth(cornerStrokeWidth);
        cornerPaint.setStrokeCap(Paint.Cap.ROUND);
        
        // 初始化文字画笔
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(textColor);
        textPaint.setTextSize(dpToPx(14));
        textPaint.setTextAlign(Paint.Align.CENTER);
        
        // 初始化遮罩画笔
        maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        maskPaint.setColor(maskColor);
        maskPaint.setStyle(Paint.Style.FILL);
        
        frameRect = new RectF();
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        
        // 计算车牌框位置（居中偏上）
        int centerX = w / 2;
        int centerY = h / 2 - dpToPx(50); // 偏上50dp
        
        frameRect.left = centerX - frameWidth / 2f;
        frameRect.top = centerY - frameHeight / 2f;
        frameRect.right = centerX + frameWidth / 2f;
        frameRect.bottom = centerY + frameHeight / 2f;
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        if (frameRect.isEmpty()) {
            return;
        }
        
        // 绘制半透明遮罩（除了车牌框区域）
        drawMask(canvas);
        
        // 绘制车牌框边框
        drawFrame(canvas);
        
        // 绘制四角标记
        drawCorners(canvas);
        
        // 绘制提示文字
        drawHintText(canvas);
    }
    
    /**
     * 绘制遮罩层
     */
    private void drawMask(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();
        
        // 创建路径，挖空车牌框区域
        Path maskPath = new Path();
        maskPath.addRect(0, 0, width, height, Path.Direction.CW);
        maskPath.addRoundRect(frameRect, dpToPx(4), dpToPx(4), Path.Direction.CCW);
        
        canvas.drawPath(maskPath, maskPaint);
    }
    
    /**
     * 绘制车牌框
     */
    private void drawFrame(Canvas canvas) {
        // 绘制圆角矩形边框
        canvas.drawRoundRect(frameRect, dpToPx(4), dpToPx(4), framePaint);
    }
    
    /**
     * 绘制四角标记
     */
    private void drawCorners(Canvas canvas) {
        float left = frameRect.left;
        float top = frameRect.top;
        float right = frameRect.right;
        float bottom = frameRect.bottom;
        
        // 左上角
        canvas.drawLine(left, top + cornerLength, left, top, cornerPaint);
        canvas.drawLine(left, top, left + cornerLength, top, cornerPaint);
        
        // 右上角
        canvas.drawLine(right - cornerLength, top, right, top, cornerPaint);
        canvas.drawLine(right, top, right, top + cornerLength, cornerPaint);
        
        // 左下角
        canvas.drawLine(left, bottom - cornerLength, left, bottom, cornerPaint);
        canvas.drawLine(left, bottom, left + cornerLength, bottom, cornerPaint);
        
        // 右下角
        canvas.drawLine(right - cornerLength, bottom, right, bottom, cornerPaint);
        canvas.drawLine(right, bottom, right, bottom - cornerLength, cornerPaint);
    }
    
    /**
     * 绘制提示文字
     */
    private void drawHintText(Canvas canvas) {
        float centerX = frameRect.centerX();
        float textY = frameRect.bottom + dpToPx(30);
        
        canvas.drawText(hintText, centerX, textY, textPaint);
    }
    
    /**
     * 设置提示文字
     */
    public void setHintText(String text) {
        this.hintText = text;
        invalidate();
    }
    
    /**
     * 设置车牌框颜色
     */
    public void setFrameColor(int color) {
        this.frameColor = color;
        framePaint.setColor(color);
        invalidate();
    }
    
    /**
     * 设置四角标记颜色
     */
    public void setCornerColor(int color) {
        this.cornerColor = color;
        cornerPaint.setColor(color);
        invalidate();
    }
    
    /**
     * 获取车牌框区域
     */
    public RectF getFrameRect() {
        return new RectF(frameRect);
    }
    
    /**
     * 开始扫描动画
     */
    public void startScanAnimation() {
        // 可以添加扫描线动画效果
        // 这里暂时不实现，可以后续扩展
    }
    
    /**
     * 停止扫描动画
     */
    public void stopScanAnimation() {
        // 停止动画
    }
    
    /**
     * dp转px
     */
    private int dpToPx(int dp) {
        return (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 
            dp, 
            getContext().getResources().getDisplayMetrics()
        );
    }
}